package com.UnionPayCenter.controller;

import com.UnionPayCenter.entity.ChannelEntity;
import com.UnionPayCenter.entity.ResponseWrapper;
import com.UnionPayCenter.entity.UserIntegralDetailEntity;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.jwt.CurrentManager;
import com.UnionPayCenter.jwt.CurrentShop;
import com.UnionPayCenter.service.IChannelService;
import com.UnionPayCenter.service.IUserIntegralDetailService;
import com.UnionPayCenter.swagger.ApiJsonObject;
import com.UnionPayCenter.swagger.ApiJsonProperty;
import com.UnionPayCenter.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: UnionPayCenter
 * @description: 管理员
 * @author: bite
 * @create:
 **/
@Slf4j
@Api(value = "userIntegralDetail", tags = "积分明细")
@RestController
@RequestMapping("/userIntegralDetail")
public class UserIntegralDetailController {
    @Autowired
    private IUserIntegralDetailService detailService;

    @CurrentManager
    @CurrentShop
    @RequestMapping(value = "/findListByUser", method = RequestMethod.POST)
    @ApiOperation(tags = "积分明细", httpMethod = "POST", value = "[后台]查询会员积分明细", response = UserIntegralDetailEntity.class)
    public ResponseWrapper findListByUser(
            @ApiJsonObject(name = "userIntegralDetailFindListByUserParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "userId", value = "会员id", type = "int"),
                    @ApiJsonProperty(key = "page", value = "页数", type = "int", required = false),
                    @ApiJsonProperty(key = "pageSize", value = "每页条数", type = "int", required = false),
                    @ApiJsonProperty(key = "startTime", value = "筛选时间起", example = "2019-04-25 08:00:00", required = false),
                    @ApiJsonProperty(key = "endTime", value = "筛选时间止", example = "2019-04-25 18:00:00", required = false)
            }) @RequestBody JSONObject jsonParam) {
        try {
            Integer userId = jsonParam.getInteger("userId");
            if (userId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "userId"));
            }
            PageInfo<UserIntegralDetailEntity> list = detailService.findListByUser(jsonParam);
            return ResponseWrapper.succeed(list);
        }catch (ApiException e){
            log.error(String.format("[后台]查询会员积分明细失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]查询会员积分明细失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
    
    @CurrentShop
    @RequestMapping(value = "/getListByUser", method = RequestMethod.POST)
    @ApiOperation(tags = "积分明细", httpMethod = "POST", value = "[前端]查询会员积分明细", response = UserIntegralDetailEntity.class)
    public ResponseWrapper getListByUser(
            @ApiJsonObject(name = "userIntegralDetailGetListByUserParams", value = {
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "userId", value = "会员id", type = "int"),
                    @ApiJsonProperty(key = "page", value = "页数", type = "int", required = false),
                    @ApiJsonProperty(key = "pageSize", value = "每页条数", type = "int", required = false),
                    @ApiJsonProperty(key = "startTime", value = "筛选时间起", example = "2019-04-25 08:00:00", required = false),
                    @ApiJsonProperty(key = "endTime", value = "筛选时间止", example = "2019-04-25 18:00:00", required = false)
            }) @RequestBody JSONObject jsonParam) {
        try {
            Integer userId = jsonParam.getInteger("userId");
            if (userId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "userId"));
            }
            PageInfo<UserIntegralDetailEntity> list = detailService.getListByUser(jsonParam);
            return ResponseWrapper.succeed(list);
        }catch (ApiException e){
            log.error(String.format("[前端]查询会员积分明细失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]查询会员积分明细失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @CurrentManager
    @CurrentShop
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiOperation(tags = "积分明细", httpMethod = "POST", value = "[后台]积分导出", response = String.class)
    public ResponseWrapper export(
            @ApiJsonObject(name = "userIntegralDetailExportParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "userId", value = "会员id", type = "int"),
                    @ApiJsonProperty(key = "startTime", value = "筛选时间起", example = "2019-04-25 08:00:00", required = false),
                    @ApiJsonProperty(key = "endTime", value = "筛选时间止", example = "2019-04-25 18:00:00", required = false)
            }) @RequestBody JSONObject jsonParam) {
        try {
            Integer userId = jsonParam.getInteger("userId");
            if (userId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "userId"));
            }
            String url = detailService.export(jsonParam);
            return ResponseWrapper.succeed(url);
        }catch (ApiException e){
            log.error(String.format("[后台]积分导出失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]积分导出失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

}
